//package com.qcxk.platform.system.component.behaviour;
//
//import com.alibaba.fastjson.JSONObject;
//import com.mysql.jdbc.StringUtils;
//import com.qcxk.platform.system.entity.Behavior;
//import com.qcxk.platform.system.service.BehaviorService;
//import com.qcxk.platform.system.utils.IPUtil;
//import com.qcxk.platform.system.utils.RedisUtil;
//import com.qcxk.platform.system.utils.ThmsUtil;
//import eu.bitwalker.useragentutils.Browser;
//import eu.bitwalker.useragentutils.UserAgent;
//import eu.bitwalker.useragentutils.Version;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.checkerframework.checker.units.qual.A;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.lang.reflect.Method;
//
//@Aspect
//@Component
//public class BehaviourAspect {
//
//   @Autowired
//   private RedisUtil redisUtil;
//
//   @Autowired
//   private BehaviorService behaviorService;
//
//   @Pointcut("@annotation(com.qcxk.platform.system.component.behaviour.BehaviourOperator)")
//   public void behaviourAspect(){}
//
//   @Around("behaviourAspect()")
//   public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
//      Object o = null;
//      try {
//         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
//                       .getRequest();
//         //获取浏览器信息
//         Browser browser = UserAgent.parseUserAgentString(request.getHeader("User-Agent")).getBrowser();
//         Version version = browser.getVersion(request.getHeader("User-Agent"));
//
//         String url = request.getRequestURL().toString();
//         String method = request.getMethod();
//         String uri = request.getRequestURI();
//         String queryString = request.getQueryString();
//
//         Behavior behavior = new Behavior();
//         //获取用户信息
//         String ip = IPUtil.getIpAdrress(request);
//         String token = request.getHeader("token");
//         o=proceedingJoinPoint.proceed();
//         JSONObject result = (JSONObject)o;
//         if("/login".equals(uri)||"/loginByAdmin".equals(uri)){
//            String code = result.getString("code");
//            behavior.setReCode(code);
//               token = result.getString("token");
//            if(!code.equals("0")){
//               String msg = result.getString("msg");
//               behavior.setReMsg(msg);
//            }
//         }
//         //获取行为信息
//         String desc = getControllerMethodDescription(proceedingJoinPoint);
//         behavior.setIp(ip);
//         behavior.setBrowserName(browser.getName());
//         behavior.setBrowserVersion(version==null?"":version.getVersion());
//         behavior.setDesctext(desc);
//         behavior.setTime(ThmsUtil.getDate());
//         behavior.setToken(token);
//
//         if(token!=null){
//            if("/exit".equals(uri)){
//               if("0".equals(result.getString("code"))){
//                  behavior.setUserId(Integer.parseInt((String)result.get("userId")));
//               }
//            }else{
//               if(null!=redisUtil.get(token)){
//                  behavior.setUserId(Integer.parseInt((String) redisUtil.get(token)));
//               }
//            }
//         }
//
//         behaviorService.save(behavior);
//      } catch (ClassNotFoundException e) {
//             e.printStackTrace();
//      }
//      return o;
//   }
//
//   /**
//     * @Description 获取注解中对方法的描述信息 用于Controller层注解
//     */
//   public static String getControllerMethodDescription(JoinPoint joinPoint) throws ClassNotFoundException {
//      String targetName = joinPoint.getTarget().getClass().getName();
//      String methodName = joinPoint.getSignature().getName();// 目标方法名
//      Object[] arguments = joinPoint.getArgs();
//      Class targetClass = Class.forName(targetName);
//      Method[] methods = targetClass.getMethods();
//      String description = "";
//      for (Method method : methods) {
//         if (method.getName().equals(methodName)) {
//            Class[] clazzs = method.getParameterTypes();
//            if (clazzs.length == arguments.length) {
//               description = method.getAnnotation(BehaviourOperator.class).desc();
//               break;
//            }
//         }
//      }
//      return description;
//   }
//}
